#include <iostream>
#include <fstream>
#include <set>
#include "BiGraph.h"

using namespace std;

bool canReach(pair<int, int> name, BiGraph< pair<int, int> >& graph) {
    BiGraph< pair<int, int> >::Vertex* v = graph.get_vertex(name);
    if (v == NULL || v->closed) return false;
    v->closed = true;

    for (BiGraph< pair<int, int> >::Out_Edge_Iterator i = v->out_edge_begin(); i != v->out_edge_end(); ++i) {
        BiGraph< pair<int, int> >::Vertex* u = i->get_vertices().second;
        if (!u->closed) { canReach(u->get_name(), graph); }
    }
    
    return true;
}

int readGraph(BiGraph< pair<int, int> >& graph) {
    ifstream input("in.txt", ifstream::in);
    if (!input.is_open()) {
        cout<<"No input file!\n";
        system("pause");
        exit(1);
    }
    
    int n = 0;
    input>>n;
    
    set< pair<int, int> > removed;
    int a, b;
    while (input.good()) {
        input>>a;
        if (a == EOF) break;
        input>>b;
        removed.insert(make_pair(a, b));
    }
    
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (removed.find(make_pair(i, j)) == removed.end()) graph.add_vertex(make_pair(i, j));
        }
    }
    
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
           // if ((i > 1) && (j > 1)) graph.add_edge_if_exist(make_pair(i, j), make_pair(i - 1, j - 1), 1);
            if (i > 1) graph.add_edge_if_exist(make_pair(i, j), make_pair(i - 1, j), 1);
            if (j > 1) graph.add_edge_if_exist(make_pair(i, j), make_pair(i, j - 1), 1);
            if (j < n-1) graph.add_edge_if_exist(make_pair(i, j), make_pair(i, j + 1), 1);
            if (i < n-1) graph.add_edge_if_exist(make_pair(i, j), make_pair(i + 1, j), 1);
            //if ((i < n-1) && (j < n-1)) graph.add_edge_if_exist(make_pair(i, j), make_pair(i + 1, j + 1), 1);
        }
    }

    return n;
}

int main() {
    BiGraph< pair<int, int> > graph(false);
    int n = readGraph(graph);
    int r = 0;
    graph.init();
    
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (canReach(make_pair(i, j), graph)) r++;
        }
    }
    
    cout<<r<<endl;
    system("pause");
    
    return 0;
}
